package lc.smart.android.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.widget.ImageView;

public class CameraIntent extends Activity
{

	public static final String TAG_STRING = "CameraIntent";//标识
	private Context mContext;
	private ImageView returnImageView;

	final static int CAMERA_RESULT = 0;

	private Uri imageUri;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_camera_intent);

		mContext = this;
		this.returnImageView = (ImageView) findViewById(R.id.iv_camera_intent_);
		//设置相片保存路径方法一
		//		String imageFilePathString = Environment.getExternalStorageDirectory()
		//				.getAbsolutePath() + "/mypicture.jpg";
		//		File imageFile = new File(imageFilePathString);
		//		imageUri = Uri.fromFile(imageFile);
		//设置保存相片路径方法二
		imageUri = Uri.parse("file:///sdcard/Camera/mypicture.jpg");
		//进入系统照相程序
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
		startActivityForResult(intent, CAMERA_RESULT);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		if (SmartApplication.is_DubugMode)
		{
			Log.i(TAG_STRING,
					"uri paht:" + imageUri.getPath() + " Authority:"
							+ imageUri.getAuthority() + " Query:"
							+ imageUri.getEncodedQuery() + " Fragment:"
							+ imageUri.getFragment() + " toString:"
							+ imageUri.toString());
			if (intent != null)
			{
				Log.i(TAG_STRING, intent.toString());
			}
			else
			{
				Log.i(TAG_STRING, "intent is null!");
			}

		}

		if (resultCode == RESULT_OK)
		{
			//获取没有保存的返回相片数据
			//Bitmap bmpBitmap = (Bitmap) intent.getExtras().get("data");
			//加载保存图片路径方式一
			//Bitmap bmpBitmap = BitmapFactory.decodeFile(imageFilePathString);
			//使用uri来提取文件路径
			//Bitmap bmpBitmap = BitmapFactory.decodeFile(imageUri.getPath());

			//加载大图片并并房子oom
			//获取屏幕大小
			Display currentDisplay = getWindowManager().getDefaultDisplay();
			int dw = currentDisplay.getWidth();
			int dh = currentDisplay.getHeight();
			//加载图像尺寸而不是图像本身
			BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
			bmpFactoryOptions.inJustDecodeBounds = true;
			Bitmap bitmap = BitmapFactory.decodeFile(imageUri.getPath(),
					bmpFactoryOptions);
			int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight
					/ (float) dh);
			int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth
					/ (float) dw);
			if (SmartApplication.is_DubugMode)
			{
				Log.i(TAG_STRING, "heightRatio:" + heightRatio + " widthRatio:"
						+ widthRatio);

			}
			//如果两个比率都大于1；
			//那么图像的一条边将大于屏幕
			if (heightRatio > 1 && widthRatio > 1)
			{
				if (heightRatio > widthRatio)
				{
					//若高度比率更大，则根据它缩放
					bmpFactoryOptions.inSampleSize = heightRatio;
				}
				else
				{
					//若宽度比率更大，则根据它缩放
					bmpFactoryOptions.inSampleSize = widthRatio;
				}
			}
			//对图像进行真正的解码
			bmpFactoryOptions.inJustDecodeBounds = false;
			bitmap = BitmapFactory.decodeFile(imageUri.getPath(),
					bmpFactoryOptions);

			returnImageView.setImageBitmap(bitmap);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.camera_intent, menu);
		return true;
	}

}
